<template>
    <div class="main-content">
        <!-- 列表页 -->
        <template v-if="showFlag">
            <el-form class="center-form-pv" inline :model="searchForm">
                <el-form-item label="医护姓名" prop="yihuxingming">
                    <el-input v-model="searchForm.yihuxingming" placeholder="医护姓名" clearable></el-input>
                </el-form-item>
                <el-form-item label="医护等级" prop="yihudengji">
                    <el-select clearable v-model="searchForm.yihudengji" placeholder="请选择医护等级">
                        <el-option v-for="(item, index) in yihudengjiOptions" :key="index" :label="item" :value="item"></el-option>
                    </el-select>
                </el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
                <el-button v-if="isAuth('yihudengji', '新增')" icon="el-icon-plus" type="success" @click="addOrUpdateHandler()"
                    >新增
                </el-button>
                <el-button
                    v-if="isAuth('yihudengji', '删除')"
                    icon="el-icon-delete"
                    :disabled="dataListSelections.length <= 0"
                    type="danger"
                    @click="deleteHandler()"
                >
                    删除
                </el-button>
            </el-form>

            <!-- <div> -->
            <el-table
                class="tables"
                v-if="isAuth('yihurenyuan', '查看')"
                :data="dataList"
                v-loading="dataListLoading"
                @selection-change="selectionChangeHandler"
            >
                <el-table-column type="selection" align="center" width="50"></el-table-column>
                <el-table-column label="序号" type="index" width="80" />
                <el-table-column :sortable="true" prop="yihugonghao" label="医护工号" />
                <el-table-column :resizable="true" :sortable="true" prop="yihuxingming" label="医护姓名" />
                <el-table-column :resizable="true" :sortable="true" prop="yihuxingbie" label="医护性别" />
                <el-table-column :resizable="true" :sortable="true" prop="zhaopian" width="200" label="照片">
                    <template slot-scope="scope">
                        <div v-if="scope.row.zhaopian">
                            <img
                                v-if="scope.row.zhaopian.substring(0, 4) === 'http'"
                                :src="scope.row.zhaopian.split(',')[0]"
                                width="100"
                                height="100"
                            />
                            <img v-else :src="$base.url + scope.row.zhaopian.split(',')[0]" width="100" height="100" />
                        </div>
                        <div v-else>无图片</div>
                    </template>
                </el-table-column>
                <el-table-column :resizable="true" :sortable="true" prop="zhicheng" label="职称" />
                <el-table-column :resizable="true" :sortable="true" prop="yihudengji" label="医护等级" />
                <el-table-column :resizable="true" :sortable="true" prop="lianxidianhua" label="联系电话" />
                <el-table-column :resizable="true" :sortable="true" prop="shenfenzheng" show-overflow-tooltip label="身份证" />
                <el-table-column width="300" label="操作">
                    <template slot-scope="scope">
                        <el-button
                            v-if="isAuth('yihurenyuan', '查看')"
                            plain
                            circle
                            type="success"
                            icon="el-icon-view"
                            size="mini"
                            @click="addOrUpdateHandler(scope.row.id, 'info')"
                        ></el-button>
                        <el-button
                            v-if="isAuth('yihurenyuan', '员工档案')"
                            plain
                            circle
                            type="info"
                            icon="el-icon-folder-opened"
                            size="mini"
                            @click="yuangongdanganCrossAddOrUpdateHandler(scope.row, 'cross', '', '', '')"
                        >
                        </el-button>
                        <el-button
                            v-if="isAuth('yihurenyuan', '修改')"
                            plain
                            circle
                            type="primary"
                            icon="el-icon-edit"
                            size="mini"
                            @click="addOrUpdateHandler(scope.row.id)"
                        ></el-button>
                        <el-button
                            v-if="isAuth('yihurenyuan', '删除')"
                            plain
                            circle
                            type="danger"
                            icon="el-icon-delete"
                            size="mini"
                            @click="deleteHandler(scope.row.id)"
                        ></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                @size-change="sizeChangeHandle"
                @current-change="currentChangeHandle"
                :current-page="pageIndex"
                background
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageSize"
                :layout="layouts.join()"
                :total="totalPage"
                prev-text="<"
                next-text=">"
                hide-on-single-page
            ></el-pagination>
            <!-- </div> -->
        </template>

        <!-- 添加/修改页面  将父组件的search方法传递给子组件-->
        <add-or-update v-if="addOrUpdateFlag" :parent="this" ref="addOrUpdate"></add-or-update>

        <yuangongdangan-cross-add-or-update
            v-if="yuangongdanganCrossAddOrUpdateFlag"
            :parent="this"
            ref="yuangongdanganCrossaddOrUpdate"
        ></yuangongdangan-cross-add-or-update>
    </div>
</template>

<script>
//$graphType1
//$buttonName1
//$subNameList1
import axios from 'axios';
import AddOrUpdate from './add-or-update';
import yuangongdanganCrossAddOrUpdate from '../yuangongdangan/add-or-update';
import { isAuth } from '@/utils/utils';

export default {
    data() {
        return {
            yihudengjiOptions: [],
            searchForm: {
                key: '',
            },
            form: {},
            dataList: [],
            pageIndex: 1,
            pageSize: 10,
            totalPage: 0,
            dataListLoading: false,
            dataListSelections: [],
            showFlag: true,
            sfshVisiable: false,
            shForm: {},
            chartVisiable: false,
            chartVisiable1: false,
            chartVisiable2: false,
            chartVisiable3: false,
            chartVisiable4: false,
            chartVisiable5: false,
            addOrUpdateFlag: false,
            yuangongdanganCrossAddOrUpdateFlag: false,
            layouts: ['total', 'prev', 'pager', 'next', 'sizes', 'jumper'],
        };
    },
    created() {
        this.init();
        this.getDataList();
        this.contentStyleChange();
    },
    mounted() {},
    filters: {
        htmlfilter: function (val) {
            return val.replace(/<[^>]*>/g).replace(/undefined/g, '');
        },
    },
    components: {
        AddOrUpdate,
        yuangongdanganCrossAddOrUpdate,
    },
    methods: {
        isAuth,
        contentStyleChange() {
            this.contentPageStyleChange();
        },
        // 分页
        contentPageStyleChange() {
            let arr = [];

            // if(this.contents.pageTotal) arr.push('total')
            // if(this.contents.pageSizes) arr.push('sizes')
            // if(this.contents.pagePrevNext){
            //   arr.push('prev')
            //   if(this.contents.pagePager) arr.push('pager')
            //   arr.push('next')
            // }
            // if(this.contents.pageJumper) arr.push('jumper')
            // this.layouts = arr.join()
            // this.contents.pageEachNum = 10
        },

        yuangongdanganCrossAddOrUpdateHandler(row, type, crossOptAudit, statusColumnName, tips, statusColumnValue) {
            this.showFlag = false;
            this.addOrUpdateFlag = false;
            this.yuangongdanganCrossAddOrUpdateFlag = true;
            this.$storage.set('crossObj', row);
            this.$storage.set('crossTable', 'yihurenyuan');
            this.$storage.set('statusColumnName', statusColumnName);
            this.$storage.set('statusColumnValue', statusColumnValue);
            this.$storage.set('tips', tips);
            if (statusColumnName !== '' && !statusColumnName.startsWith('[')) {
                var obj = this.$storage.getObj('crossObj');
                for (var o in obj) {
                    if (o === statusColumnName && obj[o] === statusColumnValue) {
                        this.$message({
                            message: tips,
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.getDataList();
                            },
                        });
                        this.showFlag = true;
                        this.yuangongdanganCrossAddOrUpdateFlag = false;
                        return;
                    }
                }
            }
            this.$nextTick(() => {
                this.$refs.yuangongdanganCrossaddOrUpdate.init(row.id, type);
            });
        },

        init() {
            this.$http({
                url: `option/yihudengji/yihudengji`,
                method: 'get',
            }).then(({ data }) => {
                if (data && data.code === 0) {
                    this.yihudengjiOptions = data.data;
                } else {
                    this.$message.error(data.msg);
                }
            });
        },
        search() {
            this.pageIndex = 1;
            this.getDataList();
        },

        // 获取数据列表
        getDataList() {
            this.dataListLoading = true;
            let params = {
                page: this.pageIndex,
                limit: this.pageSize,
                sort: 'id',
                order: 'desc',
            };
            if (this.searchForm.yihuxingming !== '' && this.searchForm.yihuxingming !== undefined) {
                params['yihuxingming'] = '%' + this.searchForm.yihuxingming + '%';
            }
            if (this.searchForm.yihudengji !== '' && this.searchForm.yihudengji !== undefined) {
                params['yihudengji'] = this.searchForm.yihudengji;
            }
            this.$http({
                url: 'yihurenyuan/page',
                method: 'get',
                params: params,
            }).then(({ data }) => {
                if (data && data.code === 0) {
                    this.dataList = data.data.list;
                    this.totalPage = data.data.total;
                } else {
                    this.dataList = [];
                    this.totalPage = 0;
                }
                this.dataListLoading = false;
            });
        },
        // 每页数
        sizeChangeHandle(val) {
            this.pageSize = val;
            this.pageIndex = 1;
            this.getDataList();
        },
        // 当前页
        currentChangeHandle(val) {
            this.pageIndex = val;
            this.getDataList();
        },
        // 多选
        selectionChangeHandler(val) {
            this.dataListSelections = val;
        },
        // 添加/修改
        addOrUpdateHandler(id, type) {
            this.showFlag = false;
            this.addOrUpdateFlag = true;
            this.crossAddOrUpdateFlag = false;
            if (type !== 'info') {
                type = 'else';
            }
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id, type);
            });
        },
        // 下载
        download(file) {
            window.open(`${file}`);
        },
        // 删除
        deleteHandler(id) {
            var ids = id
                ? [Number(id)]
                : this.dataListSelections.map(item => {
                      return Number(item.id);
                  });
            this.$confirm(`确定进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.$http({
                    url: 'yihurenyuan/delete',
                    method: 'post',
                    data: ids,
                }).then(({ data }) => {
                    if (data && data.code === 0) {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.search();
                            },
                        });
                    } else {
                        this.$message.error(data.msg);
                    }
                });
            });
        },
    },
};
</script>

<style lang="scss" scoped></style>
